<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('重大事项') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('申请标题')" prop="actTitle">
        <el-input v-model="queryParams.actTitle" :placeholder="$t('申请标题')" size="small" @keyup.enter.native="queryHandle" />
      </el-form-item>
      <el-form-item :label="$t('事项名称')" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          :placeholder="$t('事项名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('发生时间')" prop="happenTimes">
        <DatePicker
          v-model="happenTimes"
          type="daterange"
        />

      </el-form-item>
      <el-form-item :label="$t('事项类别')" prop="itemType">
        <el-select v-model="queryParams.itemType" :placeholder="$t('事项类别')" clearable>
          <el-option
            v-for="dict in itemTypeOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('是否处理')" prop="handle">
        <el-select v-model="queryParams.handle" :placeholder="$t('是否处理')" clearable>
          <el-option
            v-for="dict in handleOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:matters:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          v-hasPermi="['fund:matters:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>-->
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="fundMattersList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('申请标题')" align="center" prop="actTitle" :show-overflow-tooltip="true" min-width="240">
        <template slot-scope="scope">
          <span class="link-type" @click="updateHandle(scope.row)">{{ scope.row.actTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('事项名称')" align="center" prop="itemName" show-overflow-tooltip />
      <el-table-column :label="$t('发生时间')" align="center" prop="happenTime" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.happenTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('事项类别')" align="center" prop="itemType" :formatter="itemTypeFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('是否处理')" align="center" prop="handle" :formatter="handleFormat" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('处理日期')" align="center" prop="handleTime" show-overflow-tooltip width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('处理方式')" align="center" prop="handleMode" show-overflow-tooltip width="110" />
      <el-table-column :label="$t('审批状态')" align="center" width="120">
        <template slot-scope="scope">
          <ProcessStatus :status="scope.row.actStatus" :act-id="scope.row.actId" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="240">
        <template slot-scope="scope">
          <div style="display: flex;column-gap: 10px;justify-content: center;">
            <!-- 启动流程，终止流程，修改，删除 四个按钮 -->
            <ListBtn :bus-data="scope.row" has-permi="fund:matters" style="display: flex;" />
            <el-button
              v-if="scope.row.handle === 'N'"
              v-hasPermi="['fund:matters:edit']"
              size="small"
              type="text"
              icon="el-icon-document"
              @click="maintainMatters(scope.row)"
            >{{ $t('维护处理结果') }}</el-button>
          </div>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改重大事项对话框 -->
    <el-dialog :title="title" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <fundMattersForm ref="fundMattersForm" />
    </el-dialog>
    <!-- 维护对话框 -->
    <el-dialog width="60%" :title="$t('维护处理结果')" :visible.sync="maintain" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <div>
        <el-form ref="form" :model="form" label-width="120px" class="dataForm">
          <el-row>
            <el-col :span="12">
              <el-form-item :label="$t('处理日期')" prop="handleTime" :rules="[{required: true, message: this.$t('必填信息'), trigger: 'blur'}]">
                <DatePicker
                  v-model="form.handleTime"
                  :placeholder="$t('处理日期')"
                />

              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item :label="$t('处理方式')" prop="handleMode">
                <el-input v-model="form.handleMode" maxlength="125" show-word-limit :placeholder="$t('处理方式')" />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item :label="$t('处理描述')" prop="handleDesc">
                <el-input v-model="form.handleDesc" type="textarea" maxlength="2000" show-word-limit :placeholder="$t('处理描述')" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer" style="text-align: right;">
          <!--保存，保存并启动流程，取消 三个按钮-->
          <el-button @click="cancel">{{ $t("取消") }}</el-button>
          <el-button type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listFundMatters,
  delFundMatters,
  saveFundMatters,
  getFundMatters
} from '@/api/fund/matters/fundMatters'
import fundMattersForm from './fundMattersForm.vue'
import ProcessStatus from '@/components/Act/ProcessStatus/index'
import { downLoadExcel } from '@/utils/excelDownload'
export default {
    components: {
        fundMattersForm,
        ProcessStatus
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 重大事项表格数据
          fundMattersList: [],
          // 弹出层标题
          title: '',
          // 是否显示维护弹出层
          maintain: false,
          // 选择发生时间
          happenTimes: undefined,
          // 是否显示弹出层
          formOpen: false,
        // 事项类别 字典deal_matters_category字典
        itemTypeOptions: [],
        // 是否提交基金业协会备案字典
        fundSocietyRecordOptions: [],
        // 是否处理 字典sys_yes_no字典
        handleOptions: [],
          // 查询参数
          queryParams: {
              fundId: this.$route.query.id,
              itemName: undefined,
              happenTime: undefined,
              startDate: undefined,
              endDate: undefined,
              itemType: undefined,
              handle: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
        itemName: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], happenTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], itemType: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], fundSocietyRecord: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], handle: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ], handleTime: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ] }
      }
  },
  created() {
      this.getList()
      this.getDicts('deal_matters_category').then(response => {
          this.itemTypeOptions = response.data
      })
      this.getDicts('yes_no_record').then(response => {
          this.fundSocietyRecordOptions = response.data
      })
      this.getDicts('sys_yes_no').then(response => {
          this.handleOptions = response.data
      })
  },
  methods: {
    /** 查询重大事项列表 */
    getList() {
        this.loading = true
        if (this.happenTimes != null && this.happenTimes.length > 0) {
          this.queryParams.startDate = this.happenTimes[0]
          this.queryParams.endDate = this.happenTimes[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listFundMatters(this.queryParams).then(response => {
            this.fundMattersList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 事项类别 字典deal_matters_category字典翻译
    itemTypeFormat(row) {
      return this.selectDictLabel(this.itemTypeOptions, row.itemType)
    },
    // 是否提交基金业协会备案字典翻译
    fundSocietyRecordFormat(row) {
      return this.selectDictLabel(this.fundSocietyRecordOptions, row.fundSocietyRecord)
    },
    // 是否处理 字典sys_yes_no字典翻译
    handleFormat(row) {
      return this.selectDictLabel(this.handleOptions, row.handle)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            fundId: this.$route.query.id,
            itemName: undefined,
            happenTime: undefined,
            itemType: undefined,
            fundSocietyRecord: undefined,
            handle: 'N',
            handleTime: undefined,
            handleMode: undefined,
            handleDesc: undefined,
            itemDesc: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            actId: undefined,
            actStatus: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.happenTimes = undefined
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = this.$t('重大事项')
        this.$nextTick(() => {
            this.$refs.fundMattersForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = this.$t('重大事项')
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.fundMattersForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delFundMatters(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        downLoadExcel('/matters/fundMatters/export', this.queryParams)
    },
    maintainMatters(row) {
      getFundMatters(row.id).then(response => {
        this.form = response.data
        this.form.handle = 'Y'
      })
      this.maintain = true
    },
    // 取消按钮
    cancel() {
      this.maintain = false
      this.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.maintain = false
      // 刷新list页面
      this.getList()
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          saveFundMatters(this.form).then(response => {
            if (response.code === 200) {
              this.msgSuccess(this.$t('保存成功'))
              this.closeForm()
            } else {
              this.msgError(response.msg)
            }
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
